﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace convert
{
    public partial class convertData : common.forms.baseForm
    {
        public convertData()
        {
            InitializeComponent();
        }

        private void workerPermissionBtn_Click(object sender, EventArgs e)
        {
            try
            {
                data.convertDS.WorkerPermissionRow oldWorkerPermissionRow;
                data.convertDS.WorkerPermissionDataTable oldWorkerPermissionTbl = new data.convertDS.WorkerPermissionDataTable();
                DataAccess.DataLibs.LoadFromSQL(oldWorkerPermissionTbl, "SELECT * FROM workerPermissionOLD");

                data.masterDS.workerPermissionDataTable workerPermissionTbl = new data.masterDS.workerPermissionDataTable();
                DataAccess.DataLibs.LoadFromSQL(workerPermissionTbl, "SELECT * FROM workerPermission");
                common.myTypes.Permissions perm = new common.myTypes.Permissions();

                progressBar.Visible = true;
                progressBar.Maximum = workerPermissionTbl.Count;
                progressBar.Value = 0;

                for (int idx = 0; idx < workerPermissionTbl.Count; idx++)
                {
                    progressBar.Value++;
                    oldWorkerPermissionRow = oldWorkerPermissionTbl.FindByworkerIdcodetype(workerPermissionTbl[idx].workerId, workerPermissionTbl[idx].code, workerPermissionTbl[idx].type);
                    if (oldWorkerPermissionRow == null) continue;
                    perm.Reset();

                    string permStr = "";
                    if (oldWorkerPermissionRow.IspermissionNull() == false) permStr += oldWorkerPermissionRow.permission;
                    if (oldWorkerPermissionRow.IsitemCode2Null() == false) permStr += oldWorkerPermissionRow.itemCode2;

                    if (permStr.Contains('a')) perm.Add = true;
                    if (permStr.Contains('d')) perm.Delete = true;
                    if (permStr.Contains('e')) perm.Edit = true;
                    workerPermissionTbl[idx].permission = perm.GetPermission();
                }
                DataAccess.DataLibs.UpdateData(workerPermissionTbl);
                common.SysLibs.ShowMessage("Đã thực hiện xong");
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
            finally 
            {
                progressBar.Visible = false;
            }
        }
    }
}
